# cython: language_level=3
import numpy as np
cimport numpy as np
cimport cython

SM4_SBOX = np.array([
    0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05,
    0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99,
    0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62,
    0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA, 0x75, 0x8F, 0x3F, 0xA6,
    0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA, 0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8,
    0x68, 0x6B, 0x81, 0xB2, 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35,
    0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 0x01, 0x21, 0x78, 0x87,
    0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52, 0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E,
    0xEA, 0xBF, 0x8A, 0xD2, 0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1,
    0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30, 0xF5, 0x8C, 0xB1, 0xE3,
    0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 0xC0, 0x29, 0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F,
    0xD5, 0xDB, 0x37, 0x45, 0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C, 0x5B, 0x51,
    0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41, 0x1F, 0x10, 0x5A, 0xD8,
    0x0A, 0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0,
    0x89, 0x69, 0x97, 0x4A, 0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84,
    0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0x48
])

cdef unsigned int ROT32L(unsigned int x,unsigned int n):
    return (x >> (32 - n) | x << n) & 0xffffffff

@cython.boundscheck(False)
cdef np.ndarray[np.uint8_t, ndim=1] _sm4_key_expansion(np.ndarray[np.uint8_t, ndim=1] mkey, unsigned int round):
    cdef unsigned int FK[4]
    cdef unsigned int CK[4]
    cdef unsigned int state[4]
    cdef unsigned int x,t
    cdef np.ndarray[np.uint8_t, ndim=1] rk

    CK=[0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269]
    state=[mkey[0]<<24|mkey[1]<<16|mkey[2]<<8|mkey[3],mkey[4]<<24|mkey[5]<<16|mkey[6]<<8|mkey[7],mkey[8]<<24|mkey[9]<<16|mkey[10]<<8|mkey[11],mkey[12]<<24|mkey[13]<<16|mkey[14]<<8|mkey[15]]
    state=[state[0]^0xa3b1bac6,state[1]^0x56aa3350,state[2]^0x677d9197,state[3]^0xb27022dc]
    rk=np.zeros((4*round),np.uint8)

    for i in range(round):
        x = state[(i + 1) % 4] ^ state[(i + 2) % 4] ^ state[(i + 3) % 4] ^ CK[i]
        t = ((SM4_SBOX[(x >> 24) & 0xff]) << 24) & 0xffffffff
        t |= ((SM4_SBOX[(x >> 16) & 0xff]) << 16) & 0xffffffff
        t |= ((SM4_SBOX[(x >> 8) & 0xff]) << 8) & 0xffffffff
        t |= (SM4_SBOX[x & 0xff]) & 0xffffffff
        t = t ^ ROT32L(t, 13) ^ ROT32L(t, 23)
        state[i%4] = state[i%4] ^ t
        rk[4*i] = (state[i%4]>>24)&0xff
        rk[4*i+1] = (state[i%4]>>16)&0xff
        rk[4*i+2] = (state[i%4]>>8)&0xff
        rk[4*i+3] = state[i%4]&0xff
    return rk

def sm4_key_expansion(mkey,round):
    return _sm4_key_expansion(mkey,round)

cdef unsigned int _sm4_T(unsigned int x):
    cdef unsigned int t
    t = ((SM4_SBOX[(x >> 24) & 0xff]) << 24) & 0xffffffff
    t |= ((SM4_SBOX[(x >> 16) & 0xff]) << 16) & 0xffffffff
    t |= ((SM4_SBOX[(x >> 8) & 0xff]) << 8) & 0xffffffff
    t |= (SM4_SBOX[x & 0xff]) & 0xffffffff
    t ^= ROT32L(t, 2) ^ ROT32L(t, 10) ^ ROT32L(t, 18) ^ ROT32L(t, 24)
    return t

cdef np.ndarray[np.uint32_t, ndim=1] _sm4_enc_dec(np.ndarray[np.uint32_t, ndim=1] pt_ct, np.ndarray[np.uint32_t, ndim=1] rkey, unsigned int round):
    cdef np.ndarray[np.uint32_t, ndim=1] x
    cdef unsigned int i
    x=np.zeros((round+4),np.uint32)
    x[:4]=pt_ct
    for i in range(round):
        x[i+4]=x[i]^_sm4_T(x[i+1]^x[i+2]^x[i+3]^rkey[i])
    return x

cdef np.ndarray[np.uint8_t, ndim=2] _sm4_round_xor(np.ndarray[np.uint8_t, ndim=2] pt_ct, np.ndarray[np.uint8_t, ndim=2] rkey, unsigned int target_byte):
    cdef np.ndarray[np.uint32_t, ndim=2] pt_ct_32
    cdef np.ndarray[np.uint32_t, ndim=2] pt_ct_t
    cdef np.ndarray[np.uint32_t, ndim=2] rkey_32
    cdef np.ndarray[np.uint32_t, ndim = 2] rkey_t
    cdef np.ndarray[np.uint32_t, ndim=1] x
    cdef np.ndarray[np.uint8_t, ndim=1] xor
    cdef unsigned int i,t,round

    round=int(target_byte/4)+1
    rkey_32=np.zeros((rkey.shape[0],round),np.uint32)
    xor=np.zeros((pt_ct.shape[0]),np.uint8)
    pt_ct_t=np.array(pt_ct,np.uint32)
    pt_ct_32=np.array([pt_ct_t[:,0]<<24|pt_ct_t[:,1]<<16|pt_ct_t[:,2]<<8|pt_ct_t[:,3],pt_ct_t[:,4]<<24|pt_ct_t[:,5]<<16|pt_ct_t[:,6]<<8|pt_ct_t[:,7],pt_ct_t[:,8]<<24|pt_ct_t[:,9]<<16|pt_ct_t[:,10]<<8|pt_ct_t[:,11],pt_ct_t[:,12]<<24|pt_ct_t[:,13]<<16|pt_ct_t[:,14]<<8|pt_ct_t[:,15]],np.uint32).transpose()
    rkey_t=np.array(rkey,np.uint32)
    for i in range(round):
        rkey_32[:,i]=rkey_t[:,4*i]<<24|rkey_t[:,4*i+1]<<16|rkey_t[:,4*i+2]<<8|rkey_t[:,4*i+3]
    if rkey_32.shape[0]==1:
        for i in range(pt_ct_32.shape[0]):
            x=_sm4_enc_dec(pt_ct_32[i],rkey_32[0],round)
            t=x[-1]^x[-2]
            xor[i]=(t>>(8*(3-target_byte%4)))&0xff
    else:
        for i in range(pt_ct_32.shape[0]):
            x=_sm4_enc_dec(pt_ct_32[i],rkey_32[i],round)
            t=x[-1]^x[-2]
            xor[i]=(t>>(8*(3-target_byte%4)))&0xff
    return xor

def sm4_round_xor(pt_ct,target_byte, rkey):
    return _sm4_round_xor(pt_ct,rkey,target_byte)

cdef np.ndarray[np.uint8_t, ndim=1] _sm4_sbox_in_real(np.ndarray[np.uint8_t, ndim=2] pt_ct, np.ndarray[np.uint8_t, ndim=2] rkey, unsigned int target_byte):
    cdef np.ndarray[np.uint32_t, ndim=2] pt_ct_32
    cdef np.ndarray[np.uint32_t, ndim=2] pt_ct_t
    cdef np.ndarray[np.uint32_t, ndim=2] rkey_32
    cdef np.ndarray[np.uint32_t, ndim=2] rkey_t
    cdef np.ndarray[np.uint8_t, ndim=1] sbox_in
    cdef unsigned int i,round

    round=int(target_byte/4)+1
    if round==1:
        sbox_in=pt_ct[:,target_byte+4]^pt_ct[:,target_byte+8]^pt_ct[:,target_byte+12]^rkey[:,target_byte]
    else:
        rkey_32=np.zeros((rkey.shape[0],round),np.uint32)
        rkey_t=np.array(rkey,np.uint32)
        for i in range(round):
            rkey_32[:,i]=rkey_t[:,4*i]<<24|rkey_t[:,4*i+1]<<16|rkey_t[:,4*i+2]<<8|rkey_t[:,4*i+3]
        pt_ct_32=np.zeros((pt_ct.shape[0],3+round),np.uint32)
        pt_ct_t = np.array(pt_ct, np.uint32)
        pt_ct_32[:,:4]=np.array([pt_ct_t[:,0]<<24|pt_ct_t[:,1]<<16|pt_ct_t[:,2]<<8|pt_ct_t[:,3],pt_ct_t[:,4]<<24|pt_ct_t[:,5]<<16|pt_ct_t[:,6]<<8|pt_ct_t[:,7],pt_ct_t[:,8]<<24|pt_ct_t[:,9]<<16|pt_ct_t[:,10]<<8|pt_ct_t[:,11],pt_ct_t[:,12]<<24|pt_ct_t[:,13]<<16|pt_ct_t[:,14]<<8|pt_ct_t[:,15]],np.uint32).transpose()
        if rkey_32.shape[0]==1:
            for i in range(pt_ct.shape[0]):
                pt_ct_32[i]=_sm4_enc_dec(pt_ct_32[i,:4],rkey_32[0],round-1)
        else:
            for i in range(pt_ct.shape[0]):
                pt_ct_32[i]=_sm4_enc_dec(pt_ct_32[i,:4],rkey_32[i],round-1)
        pt_ct_32=pt_ct_32[:,-3:]
        sbox_in=np.array((((pt_ct_32[:,0]^pt_ct_32[:,1]^pt_ct_32[:,2])>>(8*(3-target_byte%4)))&0xff)^rkey[:,target_byte],np.uint8)
    return sbox_in

cdef np.ndarray[np.uint8_t, ndim=1] _sm4_sbox_in_guess(np.ndarray[np.uint8_t, ndim=2] pt_ct, np.ndarray[np.uint8_t, ndim=2] rkey, unsigned int target_byte):
    cdef np.ndarray[np.uint32_t, ndim=2] pt_ct_32
    cdef np.ndarray[np.uint32_t, ndim=2] pt_ct_t
    cdef np.ndarray[np.uint32_t, ndim=2] rkey_32
    cdef np.ndarray[np.uint32_t, ndim=2] rkey_t
    cdef np.ndarray[np.uint8_t, ndim=2] sbox_in
    cdef np.ndarray[np.uint8_t, ndim=1] guess_key
    cdef unsigned int i,round

    guess_key = np.array(range(256), np.uint8)
    round=int(target_byte/4)
    if round==0:
        sbox_in=pt_ct[:,target_byte+4]^pt_ct[:,target_byte+8]^pt_ct[:,target_byte+12]^guess_key[:,None]
        sbox_in=sbox_in.transpose()
    else:
        rkey_32=np.zeros((rkey.shape[0],round),np.uint32)
        rkey_t = np.array(rkey, np.uint32)
        for i in range(round):
            rkey_32[:,i]=rkey_t[:,4*i]<<24|rkey_t[:,4*i+1]<<16|rkey_t[:,4*i+2]<<8|rkey_t[:,4*i+3]
        pt_ct_32=np.zeros((pt_ct.shape[0],4+round),np.uint32)
        pt_ct_t=np.array(pt_ct,np.uint32)
        pt_ct_32[:,:4]=np.array([pt_ct_t[:,0]<<24|pt_ct_t[:,1]<<16|pt_ct_t[:,2]<<8|pt_ct_t[:,3],pt_ct_t[:,4]<<24|pt_ct_t[:,5]<<16|pt_ct_t[:,6]<<8|pt_ct_t[:,7],pt_ct_t[:,8]<<24|pt_ct_t[:,9]<<16|pt_ct_t[:,10]<<8|pt_ct_t[:,11],pt_ct_t[:,12]<<24|pt_ct_t[:,13]<<16|pt_ct_t[:,14]<<8|pt_ct_t[:,15]],np.uint32).transpose()
        if rkey_32.shape[0]==1:
            for i in range(pt_ct.shape[0]):
                pt_ct_32[i]=_sm4_enc_dec(pt_ct_32[i,:4],rkey_32[0],round)
        else:
            for i in range(pt_ct.shape[0]):
                pt_ct_32[i]=_sm4_enc_dec(pt_ct_32[i,:4],rkey_32[i],round)
        pt_ct_32=pt_ct_32[:,-3:]
        sbox_in=np.array((((pt_ct_32[:,0]^pt_ct_32[:,1]^pt_ct_32[:,2])>>(8*(3-target_byte%4)))&0xff)^guess_key[:,None],np.uint8)
        sbox_in=sbox_in.transpose()
    return sbox_in

def sm4_sbox_in(pt_ct,target_byte,rkey,guess=False):
    if guess:
        if rkey is None:
            return _sm4_sbox_in_guess(pt_ct,np.array([[]],np.uint8),target_byte)
        return _sm4_sbox_in_guess(pt_ct,rkey,target_byte)
    else:
        return _sm4_sbox_in_real(pt_ct,rkey,target_byte)